<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/5-14:20
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\admin\controller;


    use app\admin\service\CategoryService;
    use Jrk\Random;
    use Jrk\Strings;
    use Jrk\Trees;

    class Picture extends AdminBase
    {
        public function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub

            $this->model = app()->model("Pictures");

            $this->del = "图集";

            $this->assign("tree", Trees::toFormatTree(CategoryService::selcectAll(), 'name'));

        }


        /**
         * @return mixed
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12 0012
         * @name: addPicture
         * @describe:
         */
        public function addPicture()
        {

            if (IS_POST) {
                $data = $this->request->post();

                if (!isset($data['images']) || !is_array($data['images'])) {
                    $this->error("图集最少上传一张图片");
                } else {
                    sort($data['images']);
                }

                $validate = new \app\admin\validate\Picture();

                if (!$validate->check($data)) {
                    $this->error($validate->getError());
                }

                //首图
                $data['image_url'] = $data['images'][0];
                //图集
                $data['images'] = serialize($data['images']);
                //密码
                if ($data['is_pwd'] == 1) {
                    empty($data['pwd']) ? $data['is_pwd'] = Random::alnum(6) : $data['is_pwd'] = $data['pwd'];
                }

                unset($data['pwd']);

                $data['content'] = htmlspecialchars($data['content']);

                if (empty($data['description'])) {

                    $data['description'] = Strings::str_cut($data['content'], 100);
                }

                if (isset($data['id'])) {
                    addLog("图集修改操作", "修改图集：".$data['title']);
                    $res = $this->model->addAndEdit($data);
                } else {
                    addLog("图集添加操作", "新增图集：".$data['title']);
                    $res = $this->model->add($data);
                }

                return $res;
            }

            return $this->fetch();
        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/5
         * @name: editPicture
         * @describe:编辑
         */
        public function editPicture()
        {
            $id = $this->request->param("id/d");//父id

            $info = $this->model->getOne(['id' => $id]);

            if (empty($info)) {
                $this->assign('msg', '当前id:'.$id.'错误');
                $this->fetch("public/404");
            }
            //图集格式转换
            $info['images'] = unserialize($info['images']);

            $this->assign(compact("info"));

            return $this->fetch();
        }


        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/5
         * @name: removeCategory
         * @describe:移动
         */
        public function removeCategory()
        {
            if (IS_POST) {
                $param = $this->request->param();

                return $this->model->removeCategory($param['ids'], $param['category_id']);
            }
        }



    }